<?php

class Empeval_Methods_Request_SetMboData {
	
	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	public $mboId;
	public $values;
	
	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
		$this->mboId = $params->mboId;
		$this->values = $params->getDefault('values');
	}
	
	public function process() {
		$mbo = EmpevalMboGoalsQuery::create()
		->filterById($this->mboId)
		->useBonusGoalsFormsQuery()
			->filterByEditorId(Empeval_Auth::getInstance()->getUser()->getId())
		->endUse()
		->findOne();
		
		if (!($mbo instanceof EmpevalMboGoals))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);
		
		$con = Propel::getConnection('vgp_empeval');
		$con->beginTransaction();
		try {
			foreach ($this->values as &$value) {
				if ($value['field'] == 'weightingFactor') {
					$value['value'] = (int)$value['value'];
					if ($value['value'] < 10 || $value['value'] > 100) {
						$value['value'] = 10;
					}
				}
				EmpevalMboGoalsQuery::create()->filterById($this->mboId)->update(array(ucfirst($value['field']) => $value['value']), $con);
			}
			$con->commit();
		} catch (Exception $e) {
			$con->rollBack();
			throw $e;
		}
		
		return new Empeval_Methods_Response_SetMboData(true, $this->values);
	}
	
}